package com.chapter4.graph.sp;

import com.chapter1.basic.bag.Bag;
import com.util.In;

/**
 * 
 * @ClassName: DirectGraph
 * @Description: 有向图api(最短路径)
 * @author minjun
 * @date 2015年3月10日 下午11:34:46
 * 
 */
public class DirectWeightGraph {

	private int v;

	private Bag<DirectEdge>[] bags;

	private Bag<DirectEdge> edges;

	@SuppressWarnings("unchecked")
	public DirectWeightGraph(int v) {
		this.v = v;
		bags = (Bag<DirectEdge>[]) new Bag[v];
		edges = new Bag<DirectEdge>();
		for (int i = 0; i < v; i++) {
			bags[i] = new Bag<DirectEdge>();
		}
	}

	public DirectWeightGraph(In in) {
		this(in.readInt());
		int e = in.readInt();
		for (int i = 0; i < e; i++) {
			addEdge(new DirectEdge(in.readInt(), in.readInt(), in.readDouble()));
		}
	}

	public void addEdge(DirectEdge edge) {
		int a = edge.from();
		bags[a].add(edge);
		edges.add(edge);
	}

	public Iterable<DirectEdge> adj(int v) {
		return bags[v];
	}

	public Iterable<DirectEdge> edges() {
		return edges;
	}

	public int V() {
		return v;
	}
}
